#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from utils.DateUtil import DateUtil
import threading
import time
import json

# 上传周期报文
class Inform(threading.Thread):

    def __init__(self, logger, mqtt, deviceInfo):
        super(Inform, self).__init__()
        self.logger = logger
        self.mqtt = mqtt
        self.deviceInfo = deviceInfo

    def reSetDeviceInfo(self, deviceInfo):
        self.deviceInfo = deviceInfo

    def run(self):
        while True:
            inform = {
                "sn": self.deviceInfo["sn"],
                "aliasName": self.deviceInfo["aliasName"],
                "deviceType": self.deviceInfo["deviceType"],
                "deviceClass": self.deviceInfo["deviceClass"],
                "periodic": self.deviceInfo["periodic"],
                "uploadTime": DateUtil.getNowDate()
            }
            self.mqtt.publish("INFORM", json.dumps(inform, ensure_ascii=False), 2)
            time.sleep(self.deviceInfo["periodic"])
